package com.traffic.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;
import javax.servlet.FilterChain;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.HiddenHttpMethodFilter;

public class MainFilter extends HiddenHttpMethodFilter {

    private String methodParam = DEFAULT_METHOD_PARAM;

    public void setMethodParam(String methodParam) {
        Assert.hasText(methodParam, "'methodParam' must not be empty");
        this.methodParam = methodParam;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String _method = request.getMethod();

        String url = request.getServletPath();
        if (url.endsWith(".do") || url.indexOf(".") < 0) {
            if ("/i".equals(url) || "/f".equals(url)) {// 上传的文件
                RequestDispatcher rd = request.getRequestDispatcher("/file/content.do?path=" + url
                        + request.getPathInfo());
                rd.forward(request, response);
                return;
            }
            // 获取参数
            ReqBean reqBean = new ReqBean();
            // reqBean.setUserId(request.getHeader("userId"));
            reqBean.setUrl(url);
            try {
                
            } catch (Exception e) {// 菜单解密失败！
                throw new ServletException("非法请求！");
            }
            Cookie cookies[] = request.getCookies();
            if (cookies != null) {
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    String cookieName = cookie.getName(), cookieValue = cookie.getValue();
                    
                }
            }

            Enumeration<?> params = request.getParameterNames();
            while (params.hasMoreElements()) {
                String paramName = (String) params.nextElement();
                if (!"sid".equals(paramName)) {
                    String value = request.getParameter(paramName);
                    if ("GET".equals(_method)) {
                    	//tomcat配置URIEncoding="utf-8"则无需转码
                        //value = new String(value.getBytes("ISO8859-1"), "UTF-8");
                    }
                    reqBean.addInfo(paramName, value);
                }
            }
            request.setAttribute(ReqBean.NAME, reqBean);
        }
        String paramValue = request.getParameter(methodParam);
        if (StringUtils.hasLength(paramValue)) {
            String method = paramValue.toUpperCase(Locale.ENGLISH);
            boolean b = ("POST".equals(_method) && "PUT".equalsIgnoreCase(method))
                    || ("POST".equals(_method) && "DELETE".equalsIgnoreCase(method));
            if (b) {
                HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
                filterChain.doFilter(wrapper, response);
            } else {

            }
        } else {
            filterChain.doFilter(request, response);
        }
    }

    private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {

        private final String method;

        public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
            super(request);
            this.method = method;
        }

        @Override
        public String getMethod() {
            return this.method;
        }
    }

}
